package com.rover.system.service;

import com.grid.DataGrid;
import com.grid.Page;
import com.rover.system.entity.FormTableField;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("formTableFieldService")
@Transactional
public class FormTableFieldService extends BaseService { // extends  BaseService
    protected Logger logger = LoggerFactory.getLogger(getClass());


    @Transactional
    public DataGrid query(Page page) throws Exception {
        DataGrid dataGrid = new DataGrid();
        String hql = "  1=1 ";
        String codeId = page.getParameter("tableId");
        //   String codeValueName = page.getParameter("name");
        Map<String, Object> params = new HashMap<String, Object>();
        if (null != codeId) {
            hql += " and tableId=" + codeId;
        }
        page.setWhere(hql);
        page.setWhereParams(params);
        List<FormTableField> li = dao.findPageHql(page.getHQL(), page.getWhereParams(), page.getPageNo(),
                page.getPageSize());
        dataGrid.setResult(li);
        dataGrid.setTotalCount(dao.countHql(page.getHQLTotalCount(), page.getWhereParams()));
        return dataGrid;
    }


    /*
     * 添加的时候判断名称是否重复
     * @name 要判断的名称
     * */
    public Boolean isFieldNameRepeat(FormTableField formTableField,Integer tableId) {
        String hql = "select count(tf.fieldId) from FormTableField   as tf where tf.fieldName=:fieldName and tf.formTableInfo.tableId="+tableId;
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("fieldName", formTableField.getFieldName());
        if (formTableField.getFieldId() != null) {
            hql += " and tf.fieldId<>:fieldId";
            params.put("fieldId", formTableField.getFieldId());
        }
        if (dao.countHql(hql, params) > 0) {
            return true;
        } else
            return false;
    }

    public  List<FormTableField>   getTableField(Integer tableId) {
    	 Map<String, Object> params = new HashMap<String, Object>();
		    params.put("tableId", tableId);
            return  dao.findHql("from FormTableField where tableId=:tableId",params);
    }


}
